package cn.zqy.demo2.day01.demo01;

import java.util.Objects;

public class Demo01Main {
    public static void main(String[] args) {
       /* 1.tostring*/
        Demo01ToString a = new Demo01ToString("kkk",89);
        System.out.println(a.toString());  // 不重写就打印地址 对象的类型+@+内存地址值

        //  2. equals方法
        Demo02Equals b1 = new Demo02Equals("mimi", 10);
        Demo02Equals b2 = new Demo02Equals("nini", 20);
        //  比较地址值，equals未被重写。。方法里实际是return (this == obj)【比较地址值无意义】
        boolean isB = b1.equals(b2);
        System.out.println(isB);
        System.out.println("==================");

        // 地址赋值
        b1 = b2;
        System.out.println(b1.equals(b2));  // true

        // 3.重写equals
        Demo03OverrideEquals o = new Demo03OverrideEquals("kk",90);
        Demo03OverrideEquals o2 = new Demo03OverrideEquals("kkk",90);
        boolean equals = o.equals(o2);
        System.out.println(equals);
        System.out.println("=================");

        // 4.Objects.equals方法：防止出现空指针异常
//        String c1 = "ABC";
        String c1 = null;
        String c2 = "AbC";

          // 空指针异常，因为null不能调用一个方法
//        boolean isC = c1.equals(c2);
//        System.out.println(isC);

        boolean isD = Objects.equals(c1,c2);
        System.out.println(isD);
    }
}
